package entity

import (
	"encoding/json"
	"time"
)

//
type GuildRanking struct {
	GuildID    int32  `gorm:"primary_key"`
	ActivityID int32  `gorm:"primary_key"`
	Name       string `gorm:"size:255"`
	ServerID   int32
	Flag       int32
	Level      int32
	Date       int32 // 活动开启的日期
	Score      int64
	RecordData []byte `gorm:"type:jsonb"`
	CreatedAt  time.Time
	UpdatedAt  time.Time
	LeaderR    bool          // 会长是否领奖
	Records    map[int32]int `sql:"-"` // 领奖记录
}

func (t *GuildRanking) AfterFind() error {

	if t.Records == nil {
		t.Records = make(map[int32]int)
	}

	if len(t.RecordData) > 0 {
		return json.Unmarshal(t.RecordData, &t.Records)
	}

	return nil
}

func (t *GuildRanking) BeforeSave() error {

	if t.Records == nil {
		t.Records = make(map[int32]int)
	}

	var err error
	t.RecordData, err = json.Marshal(t.Records)

	return err
}

func (t *GuildRanking) BeforeUpdate() error {

	if t.Records == nil {
		t.Records = make(map[int32]int)
	}

	var err error
	t.RecordData, err = json.Marshal(t.Records)

	return err
}
